using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace Krile.Forms.MainForm.Controls.XTab
{
    public class XTabMouseManager : IDisposable
    {
        XTab parent;
        XTabItemBase lastPointed;
        ToolTip toolTip;
        public XTabMouseManager(XTab par)
        {
            //init valiables
            parent = par;
            lastPointed = null;
            toolTip = new ToolTip();
            toolTip.ShowAlways = true;
            toolTip.Active = false;
            //mouse event set
            parent.MouseLeave += new EventHandler(parent_MouseLeave);
            parent.MouseMove += new MouseEventHandler(parent_MouseMove);
            parent.MouseClick += new MouseEventHandler(parent_MouseClick);
            parent.MouseDoubleClick += new MouseEventHandler(parent_MouseDoubleClick);
            parent.MouseUp += new MouseEventHandler(parent_MouseUp);
            parent.MouseDown += new MouseEventHandler(parent_MouseDown);
        }

        void parent_MouseDown(object sender, MouseEventArgs e)
        {
            XTabItemBase tib = GetItemByPoint(e.Location);
            if (tib != null)
                tib.OnMouseDown(sender, e);
        }

        void parent_MouseUp(object sender, MouseEventArgs e)
        {
            XTabItemBase tib = GetItemByPoint(e.Location);
            if (tib != null)
                tib.OnMouseUp(sender, e);
        }

        void parent_MouseClick(object sender, MouseEventArgs e)
        {
            XTabItemBase tib = GetItemByPoint(e.Location);
            if (tib != null)
                tib.OnMouseClick(sender, e);
        }

        void parent_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            XTabItemBase tib = GetItemByPoint(e.Location);
            if (tib != null)
                tib.OnMouseDoubleClick(sender, e);
        }

        void parent_MouseMove(object sender, MouseEventArgs e)
        {
            XTabItemBase tib = GetItemByPoint(e.Location);
            if (tib != null)
            {
                //found
                if (!tib.Equals(lastPointed))
                {
                    releaseLastPointed();
                    lastPointed = tib;
                    toolTip.SetToolTip(parent, tib.Name);
                    toolTip.Active = true;
                    lastPointed.OnMouseEnter(sender, e);
                }
                tib.OnMouseMove(sender, e);
            }
            else
            {
                //not found
                releaseLastPointed();
            }
        }

        XTabItemBase GetItemByPoint(Point location)
        {
            foreach (XTabItemBase tib in parent.GetItemBaseArray())
                if (isPointInnerRect(location, tib.ClientRectangle))
                    return tib;
            return null;
        }

        void parent_MouseLeave(object sender, EventArgs e)
        {
            releaseLastPointed();
        }

        bool isPointInnerRect(Point pt, Rectangle rect)
        {
            if (pt.X < rect.Left || pt.X > rect.Right ||
                pt.Y < rect.Top || pt.Y > rect.Bottom)
                return false;
            else
                return true;
        }

        void releaseLastPointed()
        {
            if (lastPointed != null)
            {
                lastPointed.OnMouseLeave(parent, new MouseEventArgs(MouseButtons.None, 0, 0, 0, 0));
                lastPointed = null;
                toolTip.Active = false;
            }
        }

        public void Dispose()
        {
            toolTip.Dispose();
        }
    }
}
